package com.easy.base.controller;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import nl.captcha.Captcha;
import nl.captcha.noise.CurvedLineNoiseProducer;
import nl.captcha.text.producer.DefaultTextProducer;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.easy.base.model.Operator;
import com.easy.core.param.GlobalConst;
import com.easy.core.tools.Common;
import com.easy.core.tools.captcha.MyWordRenderer;
import com.easy.core.util.AppUtil;
import com.easy.core.web.controller.BasicController;

@Controller
public class CommonController extends BasicController {
	
	private static final char[] CAPTCHA_CHARS = new char[] { 'a', 'b', 'c', 'd','e', 'f', 'g', 'h', 'k', 
		'm', 'n', 'p', 'r', 'w', 'x', 'y','2', '3', '4', '5', '6', '7', '8', };

	@RequestMapping("/operator_login")
    public void list(HttpServletRequest request, HttpServletResponse response) {
		
		List<Operator> operList = commonService.findAll(Operator.class);
		for(Operator op : operList) {
			System.out.println(op.getId()+"***********");
		}
		JSONObject json = new JSONObject();
		json.put("b", true);
		
		try {
			response.getWriter().print(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/authCode")
	public void randomCode(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		ServletOutputStream sos = null;
		try {
			response.setContentType("image/png");
			// 设置浏览器不要缓存此图片
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "private,no-cache,no-store");
			response.setDateHeader("Expires", 0);
			
			String wordColor = AppUtil.getSysConfig().get("app.captcha.wordColor");
			String noiseColor = AppUtil.getSysConfig().get("app.captcha.noiseColor");
			
			List<Color> colors = new ArrayList<Color>();
			colors.add(convertColor(wordColor));
			MyWordRenderer my = new MyWordRenderer(colors, MyWordRenderer.DEFAULT_FONTS);
			my.setLetterSpacing(5);
			
			Captcha captcha = new Captcha.Builder(160, 50).addText(new DefaultTextProducer(4, CAPTCHA_CHARS), my).addBackground().addNoise(new CurvedLineNoiseProducer(convertColor(noiseColor), 1)).build();
			BufferedImage image = captcha.getImage();
			session.setAttribute(GlobalConst.HTTP_SESSIN_RANDOM_AUTH_CODE_KEY, captcha.getAnswer());
			
			sos = response.getOutputStream();
			ImageIO.write(image, "PNG", sos);
		} catch (SocketException e) {
			
		} catch (IOException e) {
			
		} finally {
			Common.close(log, sos);
		}
	}
	
	public Color convertColor(String argb){
		if(StringUtils.isBlank(argb)) return Color.BLACK;
		try {
			argb = argb.trim();
			return new Color(Long.valueOf(argb, 16).intValue(), 6<argb.length());
		} catch (Exception e) {
			return Color.BLACK;
		}
	}
}
